package com.zhuoyue.web.controller.system;

import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.core.page.TableDataInfo;
import com.zhuoyue.common.utils.SecurityUtils;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import com.zhuoyue.system.domain.dto.AgentStoreDTO;
import com.zhuoyue.system.domain.po.AgentStorePo;
import com.zhuoyue.system.domain.vo.AgentStoreVo;
import com.zhuoyue.system.service.IAgentStoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * 代理门店信息Controller
 *
 * @author zyzn
 * @date 2021-11-01
 */
@RestController
@RequestMapping("/system/agent/store")
public class AgentStoreController extends BaseController {
    @Autowired
    private IAgentStoreService agentStoreService;

    /**
     * 查询代理门店信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AgentStoreDTO agentStoreDTO) {
        startPage();
        List<AgentStoreVo> list = agentStoreService.selectAgentStoreList(agentStoreDtoToPo(agentStoreDTO));
        for (AgentStoreVo agentStoreVo : list) {
            agentStoreVo.setLocation(agentStoreVo.getDistrict().replace(" ", "") + agentStoreVo.getAddress() + agentStoreVo.getDetailAddress());
        }
        return getDataTable(list);
    }

    /**
     * 导出代理门店信息列表
     */
    @GetMapping("/export")
    public AjaxResult export(AgentStoreDTO agentStoreDTO) {
        List<AgentStoreVo> list = agentStoreService.selectAgentStoreList(agentStoreDtoToPo(agentStoreDTO));
        for (AgentStoreVo agentStoreVo : list) {
            agentStoreVo.setLocation(agentStoreVo.getDistrict().replace(" ", "") + agentStoreVo.getAddress() + agentStoreVo.getDetailAddress());
        }
        ExcelUtil<AgentStoreVo> util = new ExcelUtil<>(AgentStoreVo.class);
        return util.exportExcel(list, "store");
    }


    /**
     * 新增代理门店信息
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AgentStoreDTO agentStoreDTO) {
        AgentStorePo agentStorePo = agentStoreDtoToPo(agentStoreDTO);
        agentStorePo.setCreateBy(SecurityUtils.getUsername());
        return toAjax(agentStoreService.insertAgentStore(agentStorePo));
    }

    /**
     * 修改代理门店信息
     */
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody AgentStoreDTO agentStoreDTO) {
        AgentStorePo agentStorePo = agentStoreDtoToPo(agentStoreDTO);
        agentStorePo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(agentStoreService.updateAgentStore(agentStorePo));
    }

    /**
     * 删除代理门店信息
     */
    @DeleteMapping("/deleteById/{id}")
    public AjaxResult remove(@PathVariable String id) {
        return toAjax(agentStoreService.deleteAgentStoreById(id));
    }

    /**
     * dto转换为po
     *
     * @param agentStoreDTO
     * @return
     */
    private AgentStorePo agentStoreDtoToPo(AgentStoreDTO agentStoreDTO) {
        AgentStorePo agentStorePo = new AgentStorePo();
        BeanUtils.copyProperties(agentStoreDTO, agentStorePo);
        if (agentStoreDTO.getStartBusinessTime() != null) {
            agentStorePo.setStartBusinessTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(agentStoreDTO.getStartBusinessTime()), ZoneOffset.of("+8")));
        }

        return agentStorePo;
    }
}
